package dyyx.util;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import dyyx.dto.RedisClient;
import dyyx.dto.WebResult;
import redis.clients.jedis.Module;
import redis.clients.jedis.Tuple;
import redis.clients.jedis.util.Slowlog;

public abstract class RedisWebUtil {

	private static final Set<String> OPS = new HashSet<>();

	private static final String OP_CONNECT = "connect";
	private static final String OP_DIS_CONNECT = "disConnect";
	private static final String OP_CLIENT_INFO = "clientInfo";
	private static final String OP_INFO = "info";
	
	private static final String OP_memoryDoctor = "memoryDoctor";
	private static final String OP_slowLog = "slowLog";
	private static final String OP_clientList = "clientList";
	private static final String OP_moduleList = "moduleList";

	private static final String OP_type = "type";
	private static final String OP_dump = "dump";
	
	//  get lrange zcard hgetAll
	private static final String OP_get = "get";
	private static final String OP_lrange = "lrange";

	private static final String OP_hgetAll = "hgetAll";
	
	private static final String OP_scard = "scard";
	private static final String OP_smembers = "smembers";

	
	
	private static final String OP_zcard = "zcard";
	private static final String OP_zcount = "zcount";
	private static final String OP_zrangeWithScores = "zrangeWithScores";
	private static final String OP_zrevrangeWithScores = "zrevrangeWithScores";
	private static final String OP_zrangeByScoreWithScores = "zrangeByScoreWithScores";
	private static final String OP_zrevrangeByScoreWithScores = "zrevrangeByScoreWithScores";



	// slowlogGet


	static {

		OPS.add(OP_CONNECT);
		OPS.add(OP_DIS_CONNECT);
		OPS.add(OP_CLIENT_INFO);
		OPS.add(OP_INFO);
		OPS.add(OP_memoryDoctor);
		OPS.add(OP_slowLog);
		
		OPS.add(OP_clientList);
		OPS.add(OP_moduleList);
		
		OPS.add(OP_type);
		OPS.add(OP_dump);
		
		OPS.add(OP_get);
		OPS.add(OP_lrange);
		OPS.add(OP_hgetAll);
		OPS.add(OP_scard);
		OPS.add(OP_smembers);

		
		OPS.add(OP_zcount);
		OPS.add(OP_zcard);
		OPS.add(OP_zrangeWithScores);
		OPS.add(OP_zrevrangeWithScores);
		OPS.add(OP_zrangeByScoreWithScores);
		OPS.add(OP_zrevrangeByScoreWithScores);



		
	}
	
	
	public static void run(HttpServletRequest request, WebResult webResult) throws Exception {
		try {
			run_(request, webResult);
		} catch (Throwable e) {
			webResult.error = true;
			webResult.msg = webResult.op + " error," + e;
		}
	}
	
	
	private static void run_(HttpServletRequest request, WebResult webResult) throws Exception {
		String op = WebUtil.get(request, "op");
		String type = WebUtil.get(request, "type");
		String host = WebUtil.get(request, "host");
		String pwd = WebUtil.get(request, "pwd");

		int port = WebUtil.getInt(request, "port");
		int slowLogCount = WebUtil.getInt(request, "slowLogCount");
		String key = WebUtil.get(request, "key");
		///
		
		// range
		int start = WebUtil.getInt(request, "start");
		int stop = WebUtil.getInt(request, "stop");
		// score
		double min =  WebUtil.getDouble(request, "min");
		double max =  WebUtil.getDouble(request, "max");
		
		int offset = WebUtil.getInt(request, "offset");

		int count = WebUtil.getInt(request, "count");



		if(slowLogCount<=0 || slowLogCount>1000) {
			slowLogCount = 10;
		}
	
		if (!OPS.contains(op)) {
			webResult.msg = "op error";
			webResult.error = true;
			return;
		}
		webResult.op = op;
		

		if (OP_CONNECT.equals(op)) {

			RedisUtil.connect(type,host,port,pwd);
			webResult.msg = op + " success," + RedisUtil.getClient();

			return;
		}

		if (OP_DIS_CONNECT.equals(op)) {

			RedisUtil.disconnect();

			webResult.msg = op + " success";

			return;
		}

		if (OP_CLIENT_INFO.equals(op)) {
			RedisClient redisClient = RedisUtil.getClient();
			webResult.msg = redisClient.toString();
			return;
		}

		if (OP_INFO.equals(op)) {
			RedisClient redisClient = RedisUtil.getClient();
			webResult.msg = redisClient.info();
			return;
		}
		

		if (OP_memoryDoctor.equals(op)) {
			RedisClient redisClient = RedisUtil.getClient();
			webResult.msg = redisClient.memoryDoctor();
			return;
		}
		

		if (OP_clientList.equals(op)) {
			RedisClient redisClient = RedisUtil.getClient();
			webResult.msg = redisClient.clientList();
			return;
		}
		

		if (OP_moduleList.equals(op)) {
			RedisClient redisClient = RedisUtil.getClient();
			List<Module> moduleList =  redisClient.moduleList() ;
			webResult.msg = FastjsonUtil.toString(moduleList, true);
			return;
		}
		
		if (OP_slowLog.equals(op)) {
			RedisClient redisClient = RedisUtil.getClient();
			List<Slowlog>  slowlogs =  redisClient.slowlogGet() ;
			webResult.msg = FastjsonUtil.toString(slowlogs, true);
			return;
		}
		

		if (OP_type.equals(op)) {
			if(StringUtils.isBlank(key)) {
				webResult.msg = "key blank";
				return;
			}
			RedisClient redisClient = RedisUtil.getClient();
			String  keyType =  redisClient.type(key) ;
			webResult.msg = key+","+keyType;
			return;
		}
		

		if (OP_dump.equals(op)) {
			RedisClient redisClient = RedisUtil.getClient();
			byte[]  bytes =  redisClient.dump(key);
			webResult.msg = key+":"+CommUtil.toString(bytes);
			return;
		}
		
		//  get smembers lrange zcard hgetAll

		if (OP_get.equals(op)) {
			RedisClient redisClient = RedisUtil.getClient();
			String value =  redisClient.get(key);
			webResult.msg = value;
			return;
		}
		

		if (OP_lrange.equals(op)) {
			RedisClient redisClient = RedisUtil.getClient();
			List<String> values =  redisClient.lrange(key, start, stop);
			webResult.msg = StringUtils.join(values, "\n");
			return;
		}
		
		if (OP_hgetAll.equals(op)) {
			RedisClient redisClient = RedisUtil.getClient();
			Map<String,String> map =  redisClient.hgetAll(key);
			webResult.msg = FastjsonUtil.toString(map, true);
			return;
		}
		
		if (OP_smembers.equals(op)) {
			RedisClient redisClient = RedisUtil.getClient();
			Set<String> values =  redisClient.smembers(key);
			webResult.msg = StringUtils.join(values, "\n");
			return;
		}
		
		if (OP_scard.equals(op)) {
			RedisClient redisClient = RedisUtil.getClient();
			Long card =  redisClient.scard(key);	
			webResult.msg = card+"";
			return;
		}
		
		
		// zrangeWithScores zrevrangeWithScores , zrangeByScoreWithScores zrevrangeByScoreWithScores
		
		if (OP_zcard.equals(op)) {
			RedisClient redisClient = RedisUtil.getClient();
			Long card =  redisClient.zcard(key);	
			webResult.msg = card+"";
			return;
		}
		
		if (OP_zcount.equals(op)) {
			RedisClient redisClient = RedisUtil.getClient();
			Long zcount =  redisClient.zcount(key,min,max);	
			webResult.msg = zcount+"";
			return;
		}
		
		
		if (OP_zrangeWithScores.equals(op)) {
			RedisClient redisClient = RedisUtil.getClient();
			Set<Tuple> memberAndScores =  redisClient.zrangeWithScores(key,start,stop);	
			webResult.msg = FastjsonUtil.toString(memberAndScores, true);
			return;
		}
		
		if (OP_zrevrangeWithScores.equals(op)) {
			RedisClient redisClient = RedisUtil.getClient();
			Set<Tuple> memberAndScores =  redisClient.zrevrangeWithScores(key,start,stop);	
			webResult.msg = FastjsonUtil.toString(memberAndScores, true);
			return;
		}
		
		if (OP_zrangeByScoreWithScores.equals(op)) {
			RedisClient redisClient = RedisUtil.getClient();
			Set<Tuple> memberAndScores =  redisClient.zrangeByScoreWithScores(key,min,max,offset,count);	
			webResult.msg = FastjsonUtil.toString(memberAndScores, true);
			return;
		}
		
		if (OP_zrevrangeByScoreWithScores.equals(op)) {
			RedisClient redisClient = RedisUtil.getClient();
			Set<Tuple> memberAndScores =  redisClient.zrevrangeByScoreWithScores(key,min,max,offset,count);	
			webResult.msg = FastjsonUtil.toString(memberAndScores, true);
			return;
		}
		

		webResult.msg = "op error,"+op;
		
		
		
	}
	


}
